package de.cbf.cam.dsa.rcp.model.port.act;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

import de.cbf.cam.dsa.services.impl.rcp_provider.persister_service.IServiceRCPPersister;

/**
 * The activator class controls the plug-in life cycle
 */
public class ActivatorRCPImportExport extends AbstractUIPlugin {
	private static final Log logger = LogFactory.getLog(ActivatorRCPImportExport.class);
	// The plug-in ID
	public static final String PLUGIN_ID = "de.cbf.cam.dsa.rcp.model.port"; //$NON-NLS-1$

	// The shared instance
	private static ActivatorRCPImportExport plugin;

	protected IServiceRCPPersister persisterService;

	/**
	 * The constructor
	 */
	public ActivatorRCPImportExport() {
	}

	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext)
	 */
	public void start(BundleContext context) throws Exception {
		super.start(context);
		plugin = this;
		findProviderServices(context);
	}

	protected void findProviderServices(BundleContext context) {
		logger.debug("trying to find persister service...");
		findPersisterService(context);
		logger.debug("persister service aquired.");
	}
	protected void findPersisterService(BundleContext context) {
		ServiceReference<IServiceRCPPersister> serviceProviderRef = context.getServiceReference(IServiceRCPPersister.class);

	    if (serviceProviderRef != null){
	    	setPersisterService(context.getService(serviceProviderRef));
	    }
	}




	/*
	 * (non-Javadoc)
	 * @see org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext)
	 */
	public void stop(BundleContext context) throws Exception {
		plugin = null;
		super.stop(context);
	}

	/**
	 * Returns the shared instance
	 *
	 * @return the shared instance
	 */
	public static ActivatorRCPImportExport getDefault() {
		return plugin;
	}

	public IServiceRCPPersister getPersisterService() {
		return persisterService;
	}

	public void setPersisterService(IServiceRCPPersister persisterService) {
		this.persisterService = persisterService;
	}

}
